package com.jk.knowledge;

import com.jk.bean.KKnowledge;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Auser on 2016/12/8.
 */
public class KnowledgeTree {
    public static void main(String[] args) {
        List<KKnowledge> ks = getKnowledgeTree();

        System.out.println("=========>1");

    }

    public static List<KKnowledge> getKnowledgeTree() {

//        KKnowledge knowledge = new KKnowledge();
//        knowledge.setGradeBaseId(gradeId);
//        knowledge.setSubjectBaseId(subjectId);
//        knowledge.setTemplateId(templateId);
//        List<KKnowledge> knowledges = knowledgeDao.getKKnowledgeList(knowledge);
        List<KKnowledge> knowledges = new ArrayList<>();
        KKnowledge knowledge = new KKnowledge();
        knowledge.setId((long) 1223);
        knowledge.setName("三角");
        knowledge.setRootId(0);
        knowledge.setParentId((long) 0);
        knowledges.add(knowledge);


        knowledge = new KKnowledge();
        knowledge.setId((long) 1231);
        knowledge.setName("三角函数的图象与性质");
        knowledge.setRootId(1223);
        knowledge.setParentId((long) 1223);
        knowledges.add(knowledge);

        knowledge = new KKnowledge();
        knowledge.setId((long) 1236);
        knowledge.setName("三角函数模型的应用");
        knowledge.setRootId(1223);
        knowledge.setParentId((long) 1231);
        knowledge.setCompleteCount(1);
        knowledge.setHasNode(0);
        knowledges.add(knowledge);

        knowledge = new KKnowledge();
        knowledge.setId((long) 1232);
        knowledge.setName("三角函数的图象");
        knowledge.setRootId(1223);
        knowledge.setParentId((long) 1231);
        knowledge.setCompleteCount(1);
        knowledge.setHasNode(0);
        knowledges.add(knowledge);

        knowledge = new KKnowledge();
        knowledge.setId((long) 1234);
        knowledge.setName("三角函数的图象变换");
        knowledge.setRootId(1223);
        knowledge.setParentId((long) 1231);
        knowledge.setHasNode(0);
        knowledges.add(knowledge);

        knowledge = new KKnowledge();
        knowledge.setId((long) 1224);
        knowledge.setName("任意角的三角函数");
        knowledge.setRootId(1223);
        knowledge.setParentId((long) 1223);
        knowledges.add(knowledge);

        knowledge = new KKnowledge();
        knowledge.setId((long) 1228);
        knowledge.setName("三角函数线的含义与运用");
        knowledge.setRootId(1223);
        knowledge.setParentId((long) 1224);
        knowledge.setHasNode(0);
        knowledges.add(knowledge);

        knowledge = new KKnowledge();
        knowledge.setId((long) 1227);
        knowledge.setName("任意角的三角函数定义");
        knowledge.setRootId(1223);
        knowledge.setParentId((long) 1224);
        knowledge.setHasNode(0);
        knowledges.add(knowledge);

        knowledge = new KKnowledge();
        knowledge.setId((long) 1225);
        knowledge.setName("任意角的概念");
        knowledge.setRootId(1223);
        knowledge.setParentId((long) 1224);
        knowledge.setHasNode(0);
        knowledges.add(knowledge);
//
//        knowledge = new KKnowledge();
//        knowledge.setId((long) 1229);
//        knowledge.setName("同角三角函数的关系式");
//        knowledge.setRootId(1223);
//        knowledge.setParentId((long) 1224);
//        knowledges.add(knowledge);
//
//
//        knowledge = new KKnowledge();
//        knowledge.setId((long) 1268);
//        knowledge.setName("不等式");
//        knowledge.setRootId(0);
//        knowledge.setParentId((long) 0);
//        knowledges.add(knowledge);
//
//        knowledge = new KKnowledge();
//        knowledge.setId((long) 1287);
//        knowledge.setName("不等式的实际应用");
//        knowledge.setRootId(1268);
//        knowledge.setParentId((long) 1268);
//        knowledges.add(knowledge);
//
//        knowledge = new KKnowledge();
//        knowledge.setId((long) 1269);
//        knowledge.setName("不等式的性质");
//        knowledge.setRootId(1268);
//        knowledge.setParentId((long) 1268);
//        knowledges.add(knowledge);
//
//        knowledge = new KKnowledge();
//        knowledge.setId((long) 1279);
//        knowledge.setName("均值不等式及其应用");
//        knowledge.setRootId(1268);
//        knowledge.setParentId((long) 1268);
//        knowledges.add(knowledge);
//
//        knowledge = new KKnowledge();
//        knowledge.setId((long) 1280);
//        knowledge.setName("均值不等式的含义");
//        knowledge.setRootId(1268);
//        knowledge.setParentId((long) 1279);
//        knowledges.add(knowledge);
//
//        knowledge = new KKnowledge();
//        knowledge.setId((long) 1282);
//        knowledge.setName("均值不等式的实际应用");
//        knowledge.setRootId(1268);
//        knowledge.setParentId((long) 1279);
//        knowledges.add(knowledge);
//
//        knowledge = new KKnowledge();
//        knowledge.setId((long) 1281);
//        knowledge.setName("均值不等式的应用");
//        knowledge.setRootId(1268);
//        knowledge.setParentId((long) 1279);
//        knowledges.add(knowledge);

        List<KKnowledge> rootKnowledgeList = new ArrayList<KKnowledge>();
        for (KKnowledge k : knowledges) {
            if(k.getRootId() == 0){
                rootKnowledgeList.add(k);
            }
        }
        return getTree(rootKnowledgeList,knowledges);
    }

    public static List<KKnowledge> getTree(List<KKnowledge> rootList,List<KKnowledge> knowledges){
        for (KKnowledge rk : rootList) {
            List<KKnowledge> knowledgeTree = new ArrayList<KKnowledge>();
            for (KKnowledge k : knowledges) {
                if(rk.getId().equals(k.getParentId())){
                    knowledgeTree.add(k);
                }
            }
            List<KKnowledge> temps = getTree(knowledgeTree, knowledges);
            int sumTotal = 0;
            int sumComplete = 0;
            if(rk.getHasNode() == 0){
                sumTotal = rk.getTotalCount();
                sumComplete = rk.getCompleteCount();
            } else {
                for (KKnowledge t : temps){
                    if(rk.getId().equals(t.getParentId())) {
                        sumTotal += t.getTotalCount();
                        sumComplete += t.getCompleteCount();
                    }
                }
            }
            rk.setChildren(temps);
            rk.setTotalCount(sumTotal);
            rk.setCompleteCount(sumComplete);
        }
        return rootList;
    }

}
